<?php
// @formatter:off
/**
 * @file c.user-avatar.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-07 02:16:25 +0000 (Fri, 07 Jun 2013) $
 *
 * $Id: c.user-avatar.php 71 2013-06-07 02:16:25Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/extensions/UserAvatar/c.user-avatar.php $
 */
// @formatter:on

global $wcSession;

$wcThemeAssigns["PATH"] = "Avatar Editor";

if($wcSession->isLogged()) {
	global $wcDefaults;
	global $wcProfile;
	global $wcUser;
	global $wcSubUser;

	if(($wcSubUser && $wcProfile->allowedTo(WC_PERM_USER_EDIT)) || ($wcUser && $wcProfile->allowedTo(WC_PERM_USER_SELF_EDIT))) {
		$user = $wcSubUser ? $wcSubUser : $wcUser;
		//
		// Neither ROOT nor CRON can change their avatars.
		if($user->profileCode() != "ROOT") {
			$exts = array(
				"gif",
				"jpg",
				"png",
				"jpeg"
			);

			if($_SERVER["REQUEST_METHOD"] == "POST") {
				switch(	$_POST["AVT_ACTION"]) {
					case "SET":
						$user->data()->avatar = "{$_POST["AVT_TYPE"]}|{$_POST["AVT_DATA"]}";
						break;
					case "UPLOAD":
						foreach($exts as $ext) {
							$path = WCSanitizer::SystemPath("{$wcROOTDIR}/{$wcDefaults["avatar-cache"]}/{$user->id()}.{$ext}");
							if(is_readable($path)) {
								@unlink($path);
								break;
							}
						}

						$ext = explode(".", $_FILES["AVT_FILE"]["name"]);
						$ext = $ext[count($ext) - 1];

						$path = WCSanitizer::SystemPath("{$wcROOTDIR}/{$wcDefaults["avatar-cache"]}/{$user->id()}.{$ext}");
						$avatar = "U|.{$ext}";

						if(copy($_FILES["AVT_FILE"]["tmp_name"], $path)) {
							if(!cropImage($path, $wcDefaults['thumbs-size'])) {
								@unlink($path);
								$avatar = "";
							}
						}

						$user->data()->avatar = $avatar;

						break;
				}
			}

			$wcThemeAssigns["userid"] = $user->id;
			$wcThemeAssigns["useravatar"] = $user->data()->avatar();

			$list = array();
			$imgId = 0;
			//
			// Loading WComix user icons.
			if($handle = opendir(WCSanitizer::SystemPath("{$_SERVER["DOCUMENT_ROOT"]}{$wcDefaults["avatar-basic"]}"))) {
				while(false !== ($entry = readdir($handle))) {
					if(preg_match("/(user-)([0-9]{3,3})(\.)(gif|jpg|png|jpeg)/", $entry)) {
						$list["B-{$entry}"] = array(
							"id" => ++$imgId,
							"uri" => "{$wcDefaults["avatar-basic"]}/{$entry}",
							"type" => "B",
							"data" => $entry
						);
					}
				}
			}
			//
			// Loading current theme user icons, if there's any.
			if($wcDefaults["avatar-theme"] != $wcDefaults["avatar-basic"]) {
				if($handle = opendir(WCSanitizer::SystemPath("{$_SERVER["DOCUMENT_ROOT"]}{$wcDefaults["avatar-theme"]}"))) {
					while(false !== ($entry = readdir($handle))) {
						if(preg_match("/(user-)([0-9]{3,3})(\.)(gif|jpg|png|jpeg)/", $entry)) {
							$list["T-{$entry}"] = array(
								"id" => ++$imgId,
								"uri" => "{$wcDefaults["avatar-basic"]}/{$entry}",
								"type" => "T",
								"data" => $entry
							);
						}
					}
				}
			}
			//
			// Loading uploaded avatar, if theres any.
			foreach($exts as $ext) {
				$uri = "{$wcDefaults["avatar-cache"]}/{$user->id()}.{$ext}";
				if(is_readable(WCSanitizer::SystemPath("{$wcROOTDIR}/{$uri}"))) {
					$list["U-{$entry}"] = array(
						"id" => ++$imgId,
						"uri" => $uri,
						"type" => "U",
						"data" => ".{$ext}"
					);
					break;
				}
			}

			ksort($list);

			$wcThemeAssigns["avatars"] = $list;
		} else {
			$wcThemeAssigns["error-message"] = "Avatar of ROOT users can't be changed";
			require "{$this->_themeRoot}/controlers/contents/c.500.php";
		}
	} else {
		$wcThemeAssigns["error-message"] = "You don't have enough privileges";
		require "{$this->_themeRoot}/controlers/contents/c.403.php";
	}
} else {
	$wcThemeAssigns["error-message"] = "You must be logged to perform this operation";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>